package com.lebang.service.cences;

import com.lebang.domain.CencesRouseClick;
import com.lebang.domain.TaskRouseDetail;
import com.lebang.example.CencesRouseClickExample;
import com.lebang.example.TaskRouseDetailExample;
import com.lebang.mapper.CencesRouseClickMapper;
import com.lebang.mapper.MemberClidkRouseRecordMapper;
import com.lebang.mapper.TaskRouseDetailMapper;
import com.lebang.model.MyConfig;
import com.lebang.model.cences.CencesMsg;
import com.lebang.model.cences.RouseClickRecordCount;
import com.lebang.service.BaseService;
import com.lebang.service.mongodb.CencesMsgMongodb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * cences_rouse_click
 *
 * @Author xwj
 * @Date 2020-10-09
 */
@Service
public class CencesRouseClickService extends BaseService<CencesRouseClick, Integer, CencesRouseClickExample> {


    @Autowired
    private TaskRouseDetailMapper taskRouseDetailMapper;
    @Autowired
    private CencesMsgMongodb cencesMsgMongodb;
    @Autowired
    private MemberClidkRouseRecordMapper memberClidkRouseRecordMapper;
    @Autowired
    private CencesRouseClickMapper cencesRouseClickMapper;

    public void starDataInit(LocalDateTime end) {
        this.starDataInit(end, null);
    }

    /**
     * 统计指定日期的渠道效果
     *
     * @param end   结束日期
     * @param brand 渠道
     */
    public void starDataInit(LocalDateTime end, List<String> brand) {
        long starTime = System.currentTimeMillis();
        //star 开始日期  end 结束日期
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        int time = Integer.parseInt(end.toLocalDate().minusDays(1).format(df));
        LocalDateTime star = end.minusDays(1);


        //查询所有的自营任务  rouse_id 为3的是自营任务
        TaskRouseDetailExample example = new TaskRouseDetailExample();
        example.createCriteria().andRouseIdEqualTo(3L);
        List<TaskRouseDetail> rouseDetails = taskRouseDetailMapper.selectByExample(example);
        //根据日期，任务id,分组查询签到数
        List<RouseClickRecordCount> rouseClickRecordCount=memberClidkRouseRecordMapper.selectGroupByRouseIdAndDay(star,end);

        rouseClickRecordCount.forEach(s->{s.setAppRouseDays(s.getRouseId()+":"+s.getDays());});
        Map<String, List<RouseClickRecordCount>> coutMap = rouseClickRecordCount.stream().collect(Collectors.groupingBy(RouseClickRecordCount::getAppRouseDays));

        rouseDetails.forEach(s->{
             CencesRouseClick vo = new CencesRouseClick(time, MyConfig.getEnv());
             vo.setRouseId(s.getId());
             setDaysCount(vo,coutMap);
            cencesRouseClickMapper.insertAll(vo);
        });


        cencesMsgMongodb.save(new CencesMsg("自营任务签到统计", (System.currentTimeMillis() - starTime)));

    }

    private void setDaysCount(CencesRouseClick vo, Map<String, List<RouseClickRecordCount>> coutMap) {

        vo.setOneDayClick(countByRouseIdAndDays(coutMap,vo.getRouseId(),1));
        vo.setTwoDayClick(countByRouseIdAndDays(coutMap,vo.getRouseId(),2));
        vo.setSixDayClick(countByRouseIdAndDays(coutMap,vo.getRouseId(),6));
        vo.setThirteenDayClick(countByRouseIdAndDays(coutMap,vo.getRouseId(),13));
        vo.setForteenDayClick(countByRouseIdAndDays(coutMap,vo.getRouseId(),14));
        vo.setThirtyDayClick(countByRouseIdAndDays(coutMap,vo.getRouseId(),30));

    }

    private Integer countByRouseIdAndDays(Map<String, List<RouseClickRecordCount>> coutMap, Long rouseId, int i) {
        List<RouseClickRecordCount> list = coutMap.get(rouseId+":"+i);
        if(list==null){
            return 0;
        }
        Integer count =0;
        for(RouseClickRecordCount item:list){
            if(item.getDays()==i){
                count=count+item.getCount();
            }
        }
        return count;

    }


    public void deleteByTime(Integer time) {
        CencesRouseClickExample cencesRouseClickExample = new CencesRouseClickExample();
        cencesRouseClickExample.createCriteria().andTimeEqualTo(time).andAppIdEqualTo(MyConfig.getEnv());
        cencesRouseClickMapper.deleteByExample(cencesRouseClickExample);
    }
}
